package com.maxdu.labs.stt.activities;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar.Tab;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.example.stt.R;
import com.maxdu.labs.stt.TabPagerAdapter;
import com.maxdu.labs.stt.constants.Constants;
import com.maxdu.labs.stt.fragments.FeedFragment;
import com.maxdu.labs.stt.fragments.FriendsFragment;
import com.maxdu.labs.stt.fragments.TPlansFragment;
import com.maxdu.labs.stt.fragments.TrainingsFragment;

public class TestDrawerActivity extends FragmentActivity {

    private String[] mPlanetTitles;
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private CharSequence mTitle;
    private CharSequence mDrawerTitle;
    private ActionBarDrawerToggle mDrawerToggle;
    
    //TAB NAVIGATOR
    ViewPager Tab;
    TabPagerAdapter TabAdapter;
    ActionBar actionBar;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_drawer_test);
		
		//***************************************************************
				//****** [ NAVIGATE DRAWER ]
		        mPlanetTitles = getResources().getStringArray(R.array.menu_drawer);
		        mDrawerList = (ListView) findViewById(R.id.left_drawer);

		        // Set the adapter for the list view
		        mDrawerList.setAdapter(new ArrayAdapter<String>(this,
		                R.layout.row_drawer_item, mPlanetTitles));
		        // Set the list's click listener
		        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
		        
		        mTitle = mDrawerTitle = getTitle();
		        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		        
		        mDrawerToggle = new ActionBarDrawerToggle(
		                this,                  /* host Activity */
		                mDrawerLayout,         /* DrawerLayout object */
		                R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
		                R.string.drawer_open,  /* "open drawer" description */
		                R.string.drawer_close  /* "close drawer" description */
		                ) {

		            /** Called when a drawer has settled in a completely closed state. */
		            public void onDrawerClosed(View view) {
		                super.onDrawerClosed(view);
		                getActionBar().setTitle(mTitle);
		            }

		            /** Called when a drawer has settled in a completely open state. */
		            public void onDrawerOpened(View drawerView) {
		                super.onDrawerOpened(drawerView);
		                getActionBar().setTitle(mDrawerTitle);
		            }
		        };

		        // Set the drawer toggle as the DrawerListener
		        mDrawerLayout.setDrawerListener(mDrawerToggle);

		        getActionBar().setDisplayHomeAsUpEnabled(true);
		        //getActionBar().setHomeButtonEnabled(true);
		
		//***************************************************************
		//****** [ TAB NAVIGATOR ]
		
        //setContentView(R.layout.activity_main_test_menu);
        TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
        Tab = (ViewPager)findViewById(R.id.pager);
        Tab.setOnPageChangeListener(
                new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                      actionBar = getActionBar();
                      actionBar.setSelectedNavigationItem(position);
                     }
                });
        Tab.setAdapter(TabAdapter);
        actionBar = getActionBar();
        //Enable Tabs on Action Bar
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ActionBar.TabListener tabListener = new ActionBar.TabListener(){
		      @Override
		      public void onTabReselected(android.app.ActionBar.Tab tab,
		          FragmentTransaction ft) {
		    	  System.out.println("TAB (re)SELECTED === " + tab.getPosition());
		      }
		      @Override
		       public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
		              Tab.setCurrentItem(tab.getPosition());
		              System.out.println("TAB SELECTED === " + tab.getPosition());
		          }
		      @Override
		      public void onTabUnselected(android.app.ActionBar.Tab tab,
		          FragmentTransaction ft) {
		        // TODO Auto-generated method stub
		    }};
      //Add New Tab
      actionBar.addTab(actionBar.newTab().setText(getString(R.string.feed)).setTabListener(tabListener));
      actionBar.addTab(actionBar.newTab().setText(getString(R.string.tplans)).setTabListener(tabListener));
      actionBar.addTab(actionBar.newTab().setText(getString(R.string.my_trainings)).setTabListener(tabListener));
		
		
		
		//***************************************************************
		//****** [ Landing Fragment ]
        Fragment fragment = new FeedFragment();
	    FragmentManager fragmentManager = getSupportFragmentManager();
	    fragmentManager.beginTransaction()
	                   .replace(R.id.content_frame, fragment)
	                   .commit();
	}
	
	@Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Pass the event to ActionBarDrawerToggle, if it returns
        // true, then it has handled the app icon touch event
        if (mDrawerToggle.onOptionsItemSelected(item)) {
          return true;
        }
        // Handle your other action bar items...

        return super.onOptionsItemSelected(item);
    }
	
	/** Swaps fragments in the main content view */
	private void selectItem(int position) {
		
		Fragment fragment = null;
		
		switch (position) {
		case 0:
			fragment = new FriendsFragment();
			break;

		case 1:
			fragment = new TPlansFragment();
			break;
			
		case 2:
			fragment = new TrainingsFragment();
			break;
			
		default:
			break;
		}
	    
		if(fragment != null){
		    Bundle args = new Bundle();
		    //args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
		    //fragment.setArguments(args);
	
		    // Insert the fragment by replacing any existing fragment
		    FragmentManager fragmentManager = getSupportFragmentManager();
		    fragmentManager.beginTransaction()
		                   .replace(R.id.content_frame, fragment)
		                   .commit();
	
		    // Highlight the selected item, update the title, and close the drawer
		    mDrawerList.setItemChecked(position, true);
		    setTitle(mPlanetTitles[position]);
		    mDrawerLayout.closeDrawer(mDrawerList);
		}else{
			Log.e(Constants.LOG_TAG, "ERROR : fragment is NULL");
		}
	}
	
	private class DrawerItemClickListener implements ListView.OnItemClickListener {
	    @Override
	    public void onItemClick(AdapterView parent, View view, int position, long id) {
	    	System.out.println("DrawerItemClickListener :: onItemClick");
	        selectItem(position);
	    }
	}
	
	@Override
	public void setTitle(CharSequence title) {
	    mTitle = title;
	    getActionBar().setTitle(mTitle);
	}
}
